<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    
    <el-form-item label="签到状态" prop="stustate">
      <el-input v-model="dataForm.stustate" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="学号" prop="stunum">
      <el-input v-model="dataForm.stunum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="姓名" prop="stuname">
      <el-input v-model="dataForm.stuname" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="专业" prop="stusubject">
      <el-input v-model="dataForm.stusubject" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="座位号" prop="seatnum">
      <el-input v-model="dataForm.seatnum" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="地点" prop="place">
      <el-input v-model="dataForm.place" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="考场号" prop="examnum">
      <el-input v-model="dataForm.examnum" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          col: 0,
          row: '',
          stustate: '',
          stunum: '',
          stuname: '',
          stusubject: '',
          seatnum: '',
          place: '',
          examnum: ''
        },
        dataRule: {
          row: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stustate: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stunum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stuname: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          stusubject: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          seatnum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          place: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          examnum: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.col = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.col) {
            this.$http({
              url: this.$http.adornUrl(`/generator/selstu/info/${this.dataForm.col}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.row = data.selStu.row
                this.dataForm.stustate = data.selStu.stustate
                this.dataForm.stunum = data.selStu.stunum
                this.dataForm.stuname = data.selStu.stuname
                this.dataForm.stusubject = data.selStu.stusubject
                this.dataForm.seatnum = data.selStu.seatnum
                this.dataForm.place = data.selStu.place
                this.dataForm.examnum = data.selStu.examnum
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/selstu/${!this.dataForm.col ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'col': this.dataForm.col || undefined,
                'row': this.dataForm.row,
                'stustate': this.dataForm.stustate,
                'stunum': this.dataForm.stunum,
                'stuname': this.dataForm.stuname,
                'stusubject': this.dataForm.stusubject,
                'seatnum': this.dataForm.seatnum,
                'place': this.dataForm.place,
                'examnum': this.dataForm.examnum
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
